/**
 * 
 */
package com.nari.sink.utils.excelupload;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.nari.platform.model.ResponseMsg;
import com.nari.platform.parser.excelparser.PoiExcelParser;
import com.nari.platform.utils.NumberSystemConversion;
import com.nari.platform.utils.StringUtils;
import com.nari.sink.utils.excelupload.parseentity.ExcelSinkerDictEntity;

/**
 * 标准散热器解析
 * @author Xiaopeng
 * @date 2014-4-17
 */
public class SinkerDictParser {
	private List<ExcelSinkerDictEntity> excelRecordList = new Vector<ExcelSinkerDictEntity>();
	
	/**
	 * 获取标准散热器的Excel解析类的实例
	 * @return
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public static SinkerDictParser getInstance() {
		return new SinkerDictParser();
	}
	
	/**
	 * 追加Excel记录实体
	 * @param sinkerDictEntity
	 * @return
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public ResponseMsg addRecord(ExcelSinkerDictEntity sinkerDictEntity) {
		//标准散热器信息的完整性校验
		ResponseMsg rmsg = SinkerDictExcelVerify.verifyExcelRecord(sinkerDictEntity);
		if(!rmsg.isSuccess()) return rmsg;
		//追加新的标准散热器信息
		this.excelRecordList.add(sinkerDictEntity);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 解析标准散热器信息的Excel上传
	 * @param excelFile
	 * @param excelFileName
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public ResponseMsg parseSinkerDictExcel(File excelFile, String excelFileName) throws Exception {
		try {
			//读取Excel的第一张工作表
			Sheet sheet = PoiExcelParser.readExcelSheet(excelFile, excelFileName, 0);
			//循环遍历Excel的行数据
			for(int rowIndex=1; rowIndex<=sheet.getLastRowNum(); rowIndex++) {
				Row excelRow = sheet.getRow(rowIndex);
				//如果行为空白行，不进行处理
				if(PoiExcelParser.ifBlankRow(excelRow)) {
					continue;
				}
				//解析Excel的行数据
				SinkerDictExcelParserInfo parserInfo = this.parseSinkerDictRecord(rowIndex, excelRow);
				//数据解析错误
				if(!parserInfo.getParserState().isSuccess()) {
					return parserInfo.getParserState();
				} else {
					//累加Excel解析结果
					ResponseMsg rmsg = this.addRecord(parserInfo.getSinkerDictEntity());
					//Excel数据校验失败时，返回校验结果
					if(!rmsg.isSuccess()) {
						return rmsg;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		} finally {
			PoiExcelParser.freeExcelParserResource();
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 解析Excel的行数据，并返回封装的Excel记录实体
	 * @param rowIndex
	 * @param excelRow
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public SinkerDictExcelParserInfo parseSinkerDictRecord(int rowIndex, Row excelRow) throws Exception {
		ExcelSinkerDictEntity sinkerDictEntity = new ExcelSinkerDictEntity();
		ResponseMsg parserState = ResponseMsg.getInstance(true, "");
		//Excel的行统计值
		sinkerDictEntity.setRowCounter(rowIndex + 1);
		//型号
		String sinkType = PoiExcelParser.readCellContent(excelRow.getCell(0));
		if(StringUtils.isNumberic(sinkType) || StringUtils.isScientificNotation(sinkType)) {
			sinkType = NumberSystemConversion.formatToInteger(Double.parseDouble(sinkType));
		}
		sinkerDictEntity.setSinkType(StringUtils.filterSpecialChars(sinkType));
		//品牌
		String brand = PoiExcelParser.readCellContent(excelRow.getCell(1));
		if(StringUtils.isNumberic(brand) || StringUtils.isScientificNotation(brand)) {
			brand = NumberSystemConversion.formatToInteger(Double.parseDouble(brand));
		}
		sinkerDictEntity.setBrand(StringUtils.filterSpecialChars(brand));
		//高
		String sinkHeightStr = PoiExcelParser.readCellContent(excelRow.getCell(2));
		if(!"".equals(sinkHeightStr) && !StringUtils.isNumberic(sinkHeightStr)) {
			parserState = ResponseMsg.getInstance(false, "请检查Excel中行号为【"+(rowIndex+1)+"】的<高>是否为数值型!");
			return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
		} else if(StringUtils.isNumberic(sinkHeightStr)) {
			double sinkHeight = new BigDecimal(sinkHeightStr).doubleValue();
			sinkerDictEntity.setSinkHeight(sinkHeight);
		}
		//宽
		String sinkWidthStr = PoiExcelParser.readCellContent(excelRow.getCell(3));
		if(!"".equals(sinkWidthStr) && !StringUtils.isNumberic(sinkWidthStr)) {
			parserState = ResponseMsg.getInstance(false, "请检查Excel中行号为【"+(rowIndex+1)+"】的<宽>是否为数值型!");
			return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
		} else if(StringUtils.isNumberic(sinkWidthStr)) {
			double sinkWidth = new BigDecimal(sinkWidthStr).doubleValue();
			sinkerDictEntity.setSinkWidth(sinkWidth);
		}
		//长
		String sinkLengthStr = PoiExcelParser.readCellContent(excelRow.getCell(4));
		if(!"".equals(sinkLengthStr) && !StringUtils.isNumberic(sinkLengthStr)) {
			parserState = ResponseMsg.getInstance(false, "请检查Excel中行号为【"+(rowIndex+1)+"】的<长>是否为数值型!");
			return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
		} else if(StringUtils.isNumberic(sinkLengthStr)) {
			double sinkLength = new BigDecimal(sinkLengthStr).doubleValue();
			sinkerDictEntity.setSinkLength(sinkLength);
		}
		//中心矩
		String sinkCenDisStr = PoiExcelParser.readCellContent(excelRow.getCell(5));
		if(!"".equals(sinkCenDisStr) && !StringUtils.isNumberic(sinkCenDisStr)) {
			parserState = ResponseMsg.getInstance(false, "请检查Excel中行号为【"+(rowIndex+1)+"】的<中心距>是否为数值型!");
			return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
		} else if(StringUtils.isNumberic(sinkCenDisStr)) {
			double sinkCenDis = new BigDecimal(sinkCenDisStr).doubleValue();
			sinkerDictEntity.setSinkCenDis(sinkCenDis);
		}
		//单片功率
		String calPowerStr = PoiExcelParser.readCellContent(excelRow.getCell(6));
		if(!"".equals(calPowerStr) && !StringUtils.isNumberic(calPowerStr)) {
			parserState = ResponseMsg.getInstance(false, "请检查Excel中行号为【"+(rowIndex+1)+"】的<单片功率>是否为数值型!");
			return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
		} else if(StringUtils.isNumberic(calPowerStr)) {
			double calPower = new BigDecimal(calPowerStr).doubleValue();
			sinkerDictEntity.setCalPower(calPower);
		}
		//备注
		String remark = PoiExcelParser.readCellContent(excelRow.getCell(31));
		if(StringUtils.isNumberic(remark) || StringUtils.isScientificNotation(remark)) {
			remark = NumberSystemConversion.formatToDecimalNumber(Double.parseDouble(remark));
		}
		//处理特殊字符
		remark = StringUtils.filterSpecialChars(remark);
		sinkerDictEntity.setRemrak(remark);
		//构造反馈信息
		return SinkerDictExcelParserInfo.getInstance(parserState, sinkerDictEntity);
	}
	
	/**
	 * 获取标准散热器Excel解析结果
	 * @return
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public List<ExcelSinkerDictEntity> getExcelParseData() {
		return this.excelRecordList;
	}
}
